package com.bogu.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;


import com.bogu.activity.CourseActivity;
import com.bogu.constant.ConstantS;

import com.bogu.model.Course;
import com.bogu.util.AnimationUtils;
import com.bogu.util.StringUtils;
import com.example.boguplatform.R;


import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class CourseAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<Course> courses;
private DisplayMetrics dm;
private ImageLoader imageLoader=ImageLoader.getInstance();
private DisplayImageOptions options;  
private LayoutParams params;
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();  

	public CourseAdapter(Activity activity, ArrayList<Course> courses) {
	super();
	this.activity = activity;
	this.courses = courses;
	dm = new DisplayMetrics();
	activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    params=new LayoutParams(LayoutParams.MATCH_PARENT, (dm.widthPixels -30)/2);
    options = new DisplayImageOptions.Builder()  
//    .showStubImage(R.drawable.res_bg_border)
    .resetViewBeforeLoading(false)
    .showImageOnLoading(R.drawable.res_bg_border)       // 设置图片下载期间显示的图�??  
    .showImageForEmptyUri(R.drawable.res_bg_border)  // 设置图片Uri为空或是错误的时候显示的图片  
    .showImageOnFail(R.drawable.res_bg_border)       // 设置图片加载或解码过程中发生错误显示的图�??      
    .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存�??  
    .cacheOnDisc(true)                          // 设置下载的图片是否缓存在SD卡中  
//    .displayer(new RoundedBitmapDisplayer(20))  // 设置成圆角图�??  
    .build();                                   // 创建配置过得DisplayImageOption对象  
}

	@Override
	public int getCount() {
		// TODO 自动生成的方法存�?
		return courses.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO 自动生成的方法存�?
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO 自动生成的方法存�?
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO 自动生成的方法存�?
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = activity.getLayoutInflater().inflate(R.layout.course_grid_item,
					null);
			
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.addtime = (TextView) convertView.findViewById(R.id.addtime);
			holder.image=(ImageView) convertView.findViewById(R.id.image);
		convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		AnimationUtils.addTouchDark(holder.image, true);
	    holder.image.setLayoutParams(params);
		holder.name.setText(courses.get(position).getName());
		holder.addtime.setText(StringUtils.friendly_time(courses.get(position).getAdd_time()));
		if(courses.get(position).getImg_url()!=null){
		imageLoader.displayImage(ConstantS.URL_RES_ATT+"?type=course&path=" +(courses.get(position).getImg_url()).replace("/", "\\"),
				holder.image, options);}
		holder.image.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				Intent intent=new Intent(activity,CourseActivity.class);
				intent.putExtra("course",courses.get(position));
				activity.startActivity(intent);
			}
		});
		return convertView;
	}
	 private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {  
         
	        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());  
	  
	        @Override  
	        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {  
	            if (loadedImage != null) {  
	                ImageView imageView = (ImageView) view;  
	                // 是否第一次显�?  
	                boolean firstDisplay = !displayedImages.contains(imageUri);  
	                if (firstDisplay) {  
	                    // 图片淡入效果  
	                    FadeInBitmapDisplayer.animate(imageView, 500);  
	                    displayedImages.add(imageUri);  
	                }  
	            }  
	        }  
	    }		
		
	
public class ViewHolder{
	TextView name,addtime;
	ImageView image;
}
}
